<?php

require_once "Basic.php";

class RequestAllocation extends BasicEntity {
  
  public function __construct() {
    $this->fields = array (
      new FormField(true, "reserva", htmlentities("Código da Reserva"), new FormFieldAbsNumber())
    );
    $this->processFields();
  }
  
  public function actionDescription() {
    return "Request";
  }
  
  public function actionRequest() {
    $request = intval($this->fields['reserva']->value());
    
    $db = new DBUtils();
    try {
      $db->connect();
      $db->runQuery("select * from TodasReservas where idReserva = ?", array($request));
      if($db->getItem() === null) {
        $db->close();
        return htmlentities("Código da reserva não condiz com nenhum no banco de dados.");
      }
      $db->close();
      header("Location: MakeAllocation.php?idReserva=${request}");
    }
    catch(DBError $e) {
      logMessage($e->errors, "MakeAllocation");
      $db->close();
      return "Falha no banco de dados. O erro foi reportado.";
    }
  }
  
}

mustBeEmployee();
correctStdFormGET();

$page = new Page(htmlentities("Fazer uma Locação"));
$req = new RequestAllocation();
$form = new StdForm($req, $_GET, $_POST, "", "", "RequestAllocation.php");
$form->processForm();
$page->addContent($form->getHtml());

echo $page->getHtml();
